﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FreeDAM.Domain.Abstract;
using FreeDAM.Domain.Entities;
using System.Data.Entity;

namespace FreeDAM.WebUI.Areas.Admin.Controllers
{
    public class RenditionDefinitionsController : Controller
    {
        private readonly IRenditionDefinitionRepository RenditionDefinitionRepository;
        private readonly ITagRepository TagRepository;

        public RenditionDefinitionsController(IRenditionDefinitionRepository renditionDefinitionRepository, ITagRepository tagRepository)
        {
            RenditionDefinitionRepository = renditionDefinitionRepository;
            TagRepository = tagRepository;
        }

        public ActionResult Index()
        {
            return View(RenditionDefinitionRepository.All.ToList());
        }

        public ActionResult Create()
        {
            ViewBag.Tags = new MultiSelectList(TagRepository.All.Where(t => t.TagCategory.Name == "Rendition Definition").OrderBy(t => t.Name), "Id", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult Create(RenditionDefinition viewModel, int[] tagIds)
        {
            ActionResult result = View(viewModel);

            if (ModelState.IsValid)
            {
                if (!viewModel.ToFileType.StartsWith("."))
                {
                    viewModel.ToFileType = "." + viewModel.ToFileType;
                }
                if (string.IsNullOrWhiteSpace(viewModel.ToMimeType))
                {
                    viewModel.ToMimeType = Constants.MimeTypes[viewModel.ToFileType];
                }
                if (tagIds != null && tagIds.Length > 0)
                {
                    viewModel.Tags = TagRepository.All.Where(t => tagIds.Contains(t.Id)).ToList();
                }
                RenditionDefinitionRepository.InsertOrUpdate(viewModel);
                RenditionDefinitionRepository.Save();
                result = RedirectToAction("Index");
            }
            else
            {
                ViewBag.Tags = new MultiSelectList(TagRepository.All.Where(t => t.TagCategory.Name == "Rendition Definition").OrderBy(t => t.Name), "Id", "Name");
            }

            return result;
        }

        public ActionResult Edit(int id)
        {
            RenditionDefinition viewModel = RenditionDefinitionRepository.Find(id);
            ViewBag.Tags = new MultiSelectList(TagRepository.All.Where(t => t.TagCategory.Name == "Rendition Definition").OrderBy(t => t.Name), "Id", "Name", viewModel.Tags.Select(t => t.Id));

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Edit(RenditionDefinition viewModel, int[] tagIds)
        {
            ActionResult result = View(viewModel);
            RenditionDefinition renditionDefinition;

            if (ModelState.IsValid)
            {
                renditionDefinition = RenditionDefinitionRepository.Find(viewModel.Id);

                if (TryUpdateModel(renditionDefinition))
                {
                    if (!renditionDefinition.ToFileType.StartsWith("."))
                    {
                        renditionDefinition.ToFileType = "." + renditionDefinition.ToFileType;
                    }
                    if (string.IsNullOrWhiteSpace(renditionDefinition.ToMimeType))
                    {
                        renditionDefinition.ToMimeType = Constants.MimeTypes[renditionDefinition.ToFileType];
                    }
                    if (tagIds != null)
                    {
                        foreach (int tagId in tagIds)
                        {
                            if (renditionDefinition.Tags.Count(t => t.Id == tagId) == 0)
                            {
                                renditionDefinition.Tags.Add(TagRepository.Find(tagId));
                            }
                        }
                    }

                    renditionDefinition.Tags.RemoveAll(t => !tagIds.Contains(t.Id));

                    RenditionDefinitionRepository.InsertOrUpdate(renditionDefinition);
                    RenditionDefinitionRepository.Save();
                    result = RedirectToAction("Index");
                }
                else
                {
                    ViewBag.Tags = new MultiSelectList(TagRepository.All.Where(t => t.TagCategory.Name == "Rendition Definition").OrderBy(t => t.Name), "Id", "Name", viewModel.Tags.Select(t => t.Id));
                }
            }
            else
            {
                ViewBag.Tags = new MultiSelectList(TagRepository.All.Where(t => t.TagCategory.Name == "Rendition Definition").OrderBy(t => t.Name), "Id", "Name", viewModel.Tags.Select(t => t.Id));
            }

            return result;
        }
    }
}
